package com.freedom.admin.interfaces.controller;

import com.freedom.admin.application.service.cmd.PermissionCmdService;
import com.freedom.admin.application.service.query.PermissionQueryService;
import com.freedom.admin.infrastructure.persistence.pojo.form.PermissionForm;
import com.freedom.admin.infrastructure.persistence.pojo.query.PermissionPageQuery;
import com.freedom.admin.infrastructure.persistence.pojo.vo.perm.PermissionPageVO;
import com.freedom.admin.infrastructure.persistence.pojo.vo.perm.PermissionVO;
import com.freedom.common.core.base.BaseIntsForm;
import com.freedom.common.core.result.CommonResult;
import com.freedom.common.core.result.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "「管理端」权限接口")
@RestController
@RequestMapping("/api/permission")
@RequiredArgsConstructor
public class SysPermissionController {

    private final PermissionQueryService permissionQueryService;

    private final PermissionCmdService permissionCmdService;

    @ApiOperation(value = "权限分页列表")
    @GetMapping("/page")
    public PageResult<PermissionPageVO> queryPermissionPage(PermissionPageQuery permissionPageQuery) {
        return PageResult.success(permissionQueryService.queryPermissionPage(permissionPageQuery));
    }

    @ApiOperation(value = "权限列表")
    @GetMapping
    public CommonResult<List<PermissionVO>> queryPermissions(@ApiParam(value = "菜单ID") @RequestParam(required = false) Integer menuId) {
        return CommonResult.success(permissionQueryService.queryPermissions(menuId));
    }

    @ApiOperation(value = "权限详情")
    @GetMapping("/{permissionId}")
    public CommonResult<PermissionVO> findPermissionDetail(@ApiParam("权限ID") @PathVariable Integer permissionId) {
        return CommonResult.success(permissionQueryService.findPermissionDetail(permissionId));
    }

    @ApiOperation(value = "新增权限")
    @PostMapping
    public CommonResult<Boolean> addPermission(@RequestBody @Validated PermissionForm permissionForm) {
        return CommonResult.success(permissionCmdService.addPermission(permissionForm));
    }

    @ApiOperation(value = "修改权限")
    @PutMapping(value = "/{permissionId}")
    public CommonResult<Boolean> modifyPermission(@ApiParam("权限ID") @PathVariable Integer permissionId, @RequestBody PermissionForm permissionForm) {
        permissionForm.setId(permissionId);
        return CommonResult.success(permissionCmdService.modifyPermission(permissionForm));
    }

    @ApiOperation(value = "删除权限")
    @DeleteMapping("/{ids}")
    public CommonResult<Boolean> removePermissions(@RequestBody @Validated BaseIntsForm baseIntsForm) {
        return CommonResult.success(permissionCmdService.removeByIds(baseIntsForm.getIds()));
    }
}
